package com.hanrui.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.charity.zxlibrary.zxing.CaptureActivity;
import com.charity.zxlibrary.zxing.Intents;
import com.hanrui.android.R;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.fragment.MessageFragment;
import com.hanrui.android.fragment.MobileFragment;
import com.hanrui.android.fragment.PersonalFragment;
import com.hanrui.android.fragment.WorkFragment;
import com.hanrui.android.net.contract.MainContract;
import com.hanrui.android.net.presenter.MainPresenter;
import com.hanrui.android.utils.DialogHelper;
import com.hanrui.android.library.widget.TitleBar;
import com.hanrui.android.widget.bottomnavigationbar.BottomMainBar;

import java.util.List;
import java.util.Stack;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 主页
 */
public class MainActivity extends ActionBarBaseActivity<MainPresenter> implements MainContract.View {

    private Stack<Fragment> stack = new Stack<>();
    @BindView(R.id.contentContainer)
    FrameLayout contentContainer;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.bottomBar)
    BottomMainBar mBottomMainBar;
    FragmentTransaction manager;
    MessageFragment fg_message;

    WorkFragment fg_work;
    MobileFragment fg_mobile;

    PersonalFragment fg_personal;
    ImageView sao;


    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        unbinder = ButterKnife.bind(this);

        mTitleBar.setTitle("消息");
        mTitleBar.setLeftVisible(false);


        sao = (ImageView) mTitleBar.addAction(new TitleBar.ImageAction(R.mipmap.sao) {
            @Override
            public void performAction(View view) {

                permissionTask();

            }
        });

        initFragment();
        initBottomNav();
        changeFragment(0);
    }


    private void initFragment() {

        fg_message = MessageFragment.newInstance();
        fg_work = WorkFragment.newInstance();
        fg_mobile = MobileFragment.newInstance();
        fg_personal = PersonalFragment.newInstance();
        manager = getSupportFragmentManager().beginTransaction();
        manager.add(R.id.contentContainer, fg_message);
        manager.add(R.id.contentContainer, fg_work);
        manager.add(R.id.contentContainer, fg_mobile);
        manager.add(R.id.contentContainer, fg_personal);
        manager.commit();

        stack.add(fg_message);
        stack.add(fg_work);
        stack.add(fg_mobile);
        stack.add(fg_personal);

    }

    /**
     * 初始化底部导航切换事件
     */
    private void initBottomNav() {
        mBottomMainBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        mTitleBar.setTitle("消息");
                        sao.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        mTitleBar.setTitle("瀚瑞一体化");
                        sao.setVisibility(View.GONE);
                        break;
                    case 2:
                        mTitleBar.setTitle("通讯录");
                        sao.setVisibility(View.GONE);
                        break;
                    case 3:
                        mTitleBar.setTitle("我的");
                        sao.setVisibility(View.GONE);
                        break;
                }

                changeFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }


    private void permissionTask() {
        PermissionUtils.permission(PermissionConstants.CAMERA, PermissionConstants.STORAGE)
                .rationale(new PermissionUtils.OnRationaleListener() {
                    @Override
                    public void rationale(final ShouldRequest shouldRequest) {
                        DialogHelper.showRationaleDialog(shouldRequest);
                    }
                })
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(List<String> permissionsGranted) {

                        Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                        startActivityForResult(intent, Constant.REQUEST_CODE);

                    }

                    @Override
                    public void onDenied(List<String> permissionsDeniedForever,
                                         List<String> permissionsDenied) {
                        if (!permissionsDeniedForever.isEmpty()) {
                            DialogHelper.showOpenAppSettingDialog();
                        }
                        LogUtils.d(permissionsDeniedForever, permissionsDenied);
                    }
                })
                .request();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null) {
                //识别结果
                String result = data.getStringExtra(Intents.Scan.RESULT);
                mPresenter.loginRegist(MainActivity.this, result);
            }
        }
    }


    /*
        切换Tab，切换对应的Fragment
     */
    private void changeFragment(int position) {
        manager = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : stack) {
            manager.hide(fragment);
        }
        manager.show(stack.get(position));
        manager.commit();
    }


    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }


    @Override
    public void loginRegist(int code) {

        if (code == 1) {

            ToastUtils.showShort("PC端登录成功");

        } else {

            ToastUtils.showShort("PC端登录失败");
        }

    }
}
